﻿using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using SimpleTicketBooking.Models;
using SimpleTicketBooking.Models.Request;
using SimpleTicketBooking.Service;
using SimpleTicketBooking.Utils.Ext;
using System.ComponentModel;

namespace SimpleTicketBooking.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [ApiVersion((double)ApiVersions.V1)]
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
    [Route("api/[controller]/v{version:apiVersion}")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private readonly IJWT _jwt;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jwt"></param>
        public AuthController(IJWT jwt)
        {
            _jwt = jwt;
        }

        /// <summary>
        /// Generate token for user
        /// </summary>
        /// <param name="authRequest"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetToken")]
        [Description("Should generate a JWT if the validation pass")]
        public Dictionary<string, string> GetToken(AuthRequest authRequest)
        {
            //TODO validate parameters & user credentials here

            Dictionary<string, object> clims = new Dictionary<string, object>
                {
                    {"Id", 1 },
                    {"UserName", authRequest.UserName }
                };
            var result = _jwt.GetToken(clims);
            var tokenDic = new Dictionary<string, string>();
            tokenDic.Add("AuthorizationCode", result);
            tokenDic.Add("RefreshAuthorizationCode", result); // this refresh token expired time should be double as Authorization token.
            return tokenDic;
        }

        /// <summary>
        /// accept refresh authorization code to validate and generate new token for auto prolong the login status.
        /// </summary>
        /// <param name="refreshAuthorizationCode"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("RefrenshToken")]
        [Description("Should generate a new JWT if the refrenshToken is still in valid time")]
        public Dictionary<string, string> RefrenshToken(string refreshAuthorizationCode)
        {
            //TODO validate parameters & user credentials here

            var tokenDic = new Dictionary<string, string>();
            tokenDic.Add("AuthorizationCode", "AuthorizationCode");
            tokenDic.Add("RefreshAuthorizationCode", "RefreshAuthorizationCode"); // this refresh token expired time should be double as Authorization token.
            return tokenDic;
        }
    }
}
